console.log($event, '$event');
const helper = new Helper(this)
const prefix = helper.getVariable('environmentType');
const stream = StreamServices.getInstance(prefix)
const sullivan = helper.get('三维组件').widgetObj.sullivan;
const index = $($event.target).closest('.sullivan-grid-item').data('index')
const { dataTransfer } = $event;

/**
 * 刷新监控点列表
 *
 * @param projectAreaCode 区域id
 * @param projectCode 项目 id
 * @return {Promise<void>}
 */
async function handleAreaData(projectAreaCode, projectCode) {
    const { areaRows, equipmentRows, fusesRows } = await _videoAccess.queryQuotaByArea(projectAreaCode, projectCode);
    _videoAccess.renderTree([...areaRows, ...equipmentRows, ...fusesRows], projectAreaCode);
}

/**
 * 新增融合视频
 *
 * @return {Promise<*>}
 */
async function insertFuse (fuseData) {
    return _videoAccess.insertVideoFuse(fuseData).then(res => {
        const [{ projectAreaCode, projectCode }] = fuseData;
        // 刷新树
        handleAreaData(projectAreaCode, projectCode)
        layer.msg('视频融合成功!');
        return res
    });
}

function playFuse (fuseData, oldVideo) {
    helper.confirm('<div>是否需要视频拼接？</div>', {
        title: '视频拼接',
        ok($template) {
            // 关闭旧的摄像头
            sullivan.close(index);
            // 获取融合视频 URl
            _videoAccess.getFuseByEquipmentInfo(fuseData).then((res) => {
                layer.msg('拼接成功!');
                $template.remove();
                // 新增
                insertFuse(fuseData).then(result => {
                    // 获取播放地址
                    const url = stream.getStreamWebRTCPlayUrl();
                    // 拿到新增的名字，播放。
                    _videoAccess.addPlayer({
                        ...res,
                        ...result,
                        type: 'fuse',
                        // 禁用 喊话
                        shouting: false,
                        // 禁用 3D变换
                        threeD: false,
                        // 禁用 云台
                        movement: false,
                        playerType: 'webrtc',
                        equipmentName: result.fuseName || '融合预览',
                        url
                    });
                });
            });
        },
        cancel() {
            // window.layer?.msg('取消');
        }
    });
}

function findOldVideos (oldVideos) {
    return oldVideos.map(video => JSON.parse(video.equipmentInfo))
}

if (!dataTransfer?.getData('data:source')) {
    layer.msg('未找到摄像头配置!');
} else {

    const source = JSON.parse(dataTransfer?.getData('data:source'));
    // window.spliceSource = source;
    // window.spliceSullivan = sullivan;
    // window.spliceIndex = index;

    // console.log('monitorSelect: => ', source);
    if (source.children) {
        layer.msg('未找到摄像头配置!');
    } else {
        const oldVideo = sullivan.cameraMap.get(+index);
        // window.spliceOldVideo = oldVideo;
        console.log('oldVideo: => ', oldVideo, index);
        if (oldVideo?.data) {
            if (oldVideo.data.id === source.id) {
                layer.msg('您选中的是同一个视频，请重新选择!');
            } else {
                if (oldVideo.data.type === 'fuse') {
                    const oldVideos = findOldVideos(oldVideo.data.videoFuseDetails)
                    playFuse([...oldVideos, source], oldVideo)
                } else {
                    playFuse([oldVideo.data, source])
                }
            }
        } else {
            _videoAccess.addPlayer(source);
        }
    }
    // console.log(source, 'source')
}